package tictactoe;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by IntelliJ IDEA.
 * User: Double
 * Date: 16.01.12
 * Time: 4:29
 * To change this template use File | Settings | File Templates.
 */
public class ClientTest extends TestCase
{
	ServerSocket ss;
	Socket s;
	Client c;
	@Before
	public void setUp() throws Exception
	{
		ss = new ServerSocket(4020);
		s = new Socket("127.0.0.1",4020);
		c = new Client(ss.accept());
	}

	@After
	public void tearDown() throws Exception
	{
	   c.disconnect();
	   ss.close();
	}

	@Test
	public void testIsTurnFlag() throws Exception
	{
		c.setTurnFlag(true);
		Assert.assertTrue(c.isTurnFlag());

	}

	@Test
	public void testSetTurnFlag() throws Exception
	{
		c.setTurnFlag(false);
		Assert.assertFalse(c.isTurnFlag());

	}

	@Test
	public void testIsPlaying() throws Exception
	{
		Assert.assertFalse(c.isPlaying());

	}

	@Test
	public void testSetPlaying() throws Exception
	{
		c.setPlaying(true);
		Assert.assertTrue(c.isPlaying());

	}

	@Test
	public void testGetFigure() throws Exception
	{
		c.setFigure(1);
		Assert.assertEquals(1,c.getFigure());

	}

	@Test
	public void testSetFigure() throws Exception
	{
		c.setFigure(1);
		Assert.assertEquals(1,c.getFigure());

	}

	@Test
	public void testGetName() throws Exception
	{
		Assert.assertNull(c.getName());

	}

	@Test
	public void testSetName() throws Exception
	{
		c.setName("Name");
		Assert.assertEquals("Name",c.getName());

	}

	@Test
	public void testSendMsg() throws Exception
	{
		c.sendMsg("lol");

	}

	@Test
	public void testDisconnect() throws Exception
	{
		c.disconnect();
		Assert.assertTrue(c.disconnected());

	}

	@Test
	public void testHasMessage() throws Exception
	{
		Assert.assertFalse(c.hasMessage());

	}

	@Test
	public void testGetMessage() throws Exception
	{
		s.getOutputStream().write("hello\n".getBytes());
		Assert.assertEquals("hello",c.getMessage());

	}

	@Test
	public void testDisconnected() throws Exception
	{
		c.disconnect();
		Assert.assertTrue(c.disconnected());

	}
}
